# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.industryDao import IndustryDao

from . import app

CORS(app, supports_credentials=True)

'''项目行业表 zzh_industry API'''


@app.route('/add-industry', methods=['POST'])
def add_industry():
    if not request.json:
        abort(400)
    indus_info = {
        "industry_name": request.json['industry_name'],
        "industry_vid": request.json['industry_vid'],
        "industry_code": request.json['industry_code'],
        "industry_status": request.json.get('industry_status', 1),
        "industry_sort": request.json.get('industry_sort', 0)
    }
    result = IndustryDao().add_industry(indus_info)
    if result:
        return jsonify({"indusInfo": indus_info, "message": {"code": 1, "info": "添加成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "添加失败！"}})


@app.route('/update-industry/<int:indus_id>', methods=['POST'])
def update_indus(indus_id):
    if not request.json:
        abort(400)
    indus_info = {
        "industry_name": request.json['industry_name'],
        "industry_vid": request.json['industry_vid'],
        "industry_code": request.json['industry_code'],
        "industry_status": request.json.get('industry_status', 1),
        "industry_sort": request.json.get('industry_sort', 0)
    }
    result = IndustryDao().update_indus(indus_id, indus_info)
    return jsonify({"indusInfo": indus_info, "message": {"code": 1, "info": "更新成功！"}})


@app.route('/query-indus', methods=['GET'])
def query_indus():
    query_result = IndustryDao().query_indus()

    return jsonify({"indusInfo": query_result, "message": {"code": 1, "info": "OK"}})
